package iotwisp.school.api.controller.app_user;

import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.query.pagination.Pagination;
import iotwisp.base.rest.JSONResultDTO;
import iotwisp.base.rest.RestBaseController;
import iotwisp.base.util.JSONUtils;
import iotwisp.school.user.entity.Permission;
import iotwisp.school.user.entity.UserPcMenu;
import iotwisp.school.user.qo.UserPcMenuQO;
import iotwisp.school.user.service.UserPcMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("appUserMenuController")
@RequestMapping("/api/app/menu")
public class UserMenuController extends RestBaseController {

    @Autowired
    private UserPcMenuService userPcMenuService;

    @RequestMapping(value = "/query", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<JSONResultDTO> queryUserMenu(@RequestBody(required = false) UserPcMenuQO qoBody,
                                                   @RequestParam(name = "qo", required = false) String qoJson) throws UnsupportedEncodingException {

        UserPcMenuQO qo;
        if (!StringUtils.isBlank(qoJson)) {
            qo = JSONUtils.parseObject(URLDecoder.decode(qoJson, "UTF-8"), UserPcMenuQO.class);
        } else {
            qo = qoBody;
        }

        switch (qo.getResultType()) {
            case UserPcMenuQO.RESULT_TYPE_COUNT:
                Integer count = userPcMenuService.queryCount(qo);
                return ResponseEntity.ok(createSuccessResult(count));
            case UserPcMenuQO.RESULT_TYPE_UNIQUE:
                UserPcMenu userPcMenu = userPcMenuService.queryUnique(qo);
                return ResponseEntity.ok(createSuccessResult(userPcMenu));
            case UserPcMenuQO.RESULT_TYPE_LIST:
                List<UserPcMenu> userPcMenus = userPcMenuService.queryList(qo);
                if (qo.getReturnTree()) {
                    userPcMenus = transToTree(userPcMenus);
                }
                return ResponseEntity.ok(createSuccessResult(userPcMenus));
            case UserPcMenuQO.RESULT_TYPE_PAGINATION:
                Pagination pagination = userPcMenuService.queryPagination(qo);
                return ResponseEntity.ok(createSuccessResult(pagination));
            default:
                return ResponseEntity.badRequest()
                        .body(createFailResult(BusinessException.NECESSARY_PARAM_NULL, "resultType值有误", null));
        }

    }

    private List<UserPcMenu> transToTree(List<UserPcMenu> userPcMenus) {

        List<UserPcMenu> list = new ArrayList<>();
        Map<String, List<Permission>> userPcMenuChildren = new HashMap<>();

        for (UserPcMenu userPcMenu : userPcMenus) {
            if (!StringUtils.isBlank(userPcMenu.getTreeData().getParentId())) {
                if (!userPcMenuChildren.containsKey(userPcMenu.getTreeData().getParentId())) {
                    userPcMenuChildren.put(userPcMenu.getTreeData().getParentId(), new ArrayList<>());
                }
                userPcMenuChildren.get(userPcMenu.getTreeData().getParentId()).add(userPcMenu);
            }
        }

        for (UserPcMenu userPcMenu : userPcMenus) {
            if (StringUtils.isBlank(userPcMenu.getTreeData().getParentId())) {
                userPcMenu.setChildren(userPcMenuChildren.get(userPcMenu.getId()));
                list.add(userPcMenu);
            }
        }
        return list;
    }

}
